﻿using ZeroSum.Constants;
using ZeroSum.DependencyInjection.Attributes;
using ZeroSum.Domain.Repositories;
using ZeroSum.Plugin.Authorization.Datas.Aggregates.UserAggregate;
using ZeroSumZSA.Core.Aggregates.RoleAggregate;
using ZeroSumZSA.Core.Aggregates.UserAggregate;

namespace ZeroSumZSA.Core.DomainServices.Users;

[Register]
public class UserDataSeedProvider : IDataSeedProvider
{
    private readonly IRepository<User> _repository;
    private readonly IRepository<Role> _roleRepository;

    public UserDataSeedProvider(IRepository<User> repository, IRepository<Role> roleRepository)
    {
        _repository = repository;
        _roleRepository = roleRepository;
    }

    public async Task GenerateAsync()
    {
        if (await _repository.CountAsync() <= 0)
        {
            var roles = await _roleRepository.GetListAsync(QueryByEntity<Role>.Of());
            var users = new List<User>
            {
                new()
                {
                    Id = 142307070910551,
                    Account = "superadmin",
                    Name = "超级管理员",
                    NickName = "superadmin",
                    NormalizeAccount = "superadmin",
                    Password = "e10adc3949ba59abbe56e057f20f883e",
                    IsAdmin = true,
                    Email = "278100426@qq.com",
                    Birthday = DateTime.Today,
                    Phone = "18692350729",
                    Sex = Gender.Male,
                    Roles = roles
                },
                new()
                {
                    Id = 142307070910552,
                    Account = "admin",
                    Name = "系统管理员",
                    NormalizeAccount = "admin",
                    NickName = "admin",
                    Password = "e10adc3949ba59abbe56e057f20f883e",
                    IsAdmin = false,
                    Email = "278100426@qq.com",
                    Birthday = DateTime.Today,
                    Phone = "18692350729",
                    Sex = Gender.Male,
                    Roles = roles
                },
                new()
                {
                    Id = 142307070910553,
                    Account = "test",
                    Name = "普通用户",
                    NormalizeAccount = "test",
                    NickName = "test",
                    Email = "278100426@qq.com",
                    Password = "e10adc3949ba59abbe56e057f20f883e",
                    IsAdmin = false,
                    Birthday = DateTime.Today,
                    Phone = "18692350729",
                    Sex = Gender.Male,
                    Roles = roles
                }
            };

            await _repository.AddRangeAsync(users);
            await _repository.SaveChangesAsync();
        }
    }

    public int Order { get; set; } = 3;
}